package com.jingdianjichi.subject.infra.basic.service.impl;

import com.alibaba.fastjson.JSON;
import com.jingdianjichi.subject.common.enums.ISDeletedFlagEnum;
import com.jingdianjichi.subject.infra.basic.entity.SubjectCategory;
import com.jingdianjichi.subject.infra.basic.mapper.SubjectCategoryDao;
import com.jingdianjichi.subject.infra.basic.service.SubjectCategoryService;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 题目分类(SubjectCategory)表服务实现类
 *
 * @author makejava
 * @since 2024-01-05 16:01:05
 */
@Service("subjectCategoryService")
@Slf4j
public class SubjectCategoryServiceImpl implements SubjectCategoryService {
    @Resource
    private SubjectCategoryDao subjectCategoryDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public SubjectCategory queryById(Long id) {
        return this.subjectCategoryDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param subjectCategory 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */


    /**
     * 新增数据
     *
     * @param subjectCategory 实例对象
     * @return 实例对象
     */
    @Override
    public SubjectCategory insert(SubjectCategory subjectCategory) {
        this.subjectCategoryDao.insert(subjectCategory);
        return subjectCategory;
    }

    /**
     * 修改数据
     *
     * @param subjectCategory 实例对象
     * @return 实例对象
     */
    @Override
    public SubjectCategory update(SubjectCategory subjectCategory) {
        this.subjectCategoryDao.update(subjectCategory);
        return this.queryById(subjectCategory.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        return this.subjectCategoryDao.deleteById(id) > 0;
    }


    @Override
    public List<SubjectCategory> queryCategory(SubjectCategory subjectCategory) {
        if (log.isInfoEnabled()){
            log.info("queryCategory--SubjectCategoryService:{}", JSON.toJSONString(subjectCategory));
        }
        List<SubjectCategory> subjectCategoryList= null;
        try {
            subjectCategoryList = subjectCategoryDao.queryCategory(subjectCategory);
            return subjectCategoryList;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void updateCategory(SubjectCategory subjectCategory) {
        if (log.isInfoEnabled()){
            log.info("updateCategory--SubjectCategoryService:{}", JSON.toJSONString(subjectCategory));
        }
        try {
            subjectCategoryDao.update(subjectCategory);
        } catch (Exception e) {
            log.info("Messange---updateCategory--SubjectCategoryService:{}",e.getMessage());
            throw new RuntimeException(e);
        }
    }

    @Override
    public void deleteCategory(SubjectCategory subjectCategory) throws Exception {
        if (log.isInfoEnabled()){
            log.info("deleteCategory--SubjectCategoryService:{}", JSON.toJSONString(subjectCategory));
        }
        SubjectCategory subjectCategoryNew = subjectCategoryDao.queryById(subjectCategory.getId());
        if (subjectCategoryNew.getParentId()==0){
            throw new Exception("不能直接删除父分类");
        }else
            subjectCategory.setIsDeleted(ISDeletedFlagEnum.DELETED.getCode());
            subjectCategoryDao.update(subjectCategory);
        }
    }

